package com.mlick.a.csnotes;

import java.util.Arrays;

/**
 * @author lixiangxin
 * @date 2019/12/11 10:16
 **/
public class T4 {

    /**
     * 运行时间：16ms
     * 占用内存：9192k
     * @param A
     * @return
     */
    public int[] multiply(int[] A) {
        int length = A.length;
        int[] B = new int[length];
        for (int i = 0; i < length; i++) {
            B[i] = recursive(A, 0, i - 1) * recursive(A, i + 1, length - 1);
        }

        return B;
    }

    private int recursive(int[] a, int s, int e) {
        if (s == e) {
            return a[s];
        }
        if (s > e) {
            return 1;
        }
        return a[s] * recursive(a, s + 1, e);
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new T4().multiply(new int[]{1, 2, 3, 4, 5})));

        //[1,2,3,4,5]
        //[120,60,40,30,24]
        //A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1]
        //1 1*2*3*4*5 = 120
        //2 1*
    }
}
